package com.m.controller;

import com.m.enums.WebReturnCode;
import com.m.pojo.dto.blackwhite.AddBlackWhiteInfoDto;
import com.m.pojo.dto.blackwhite.BlackWhiteInfoDto;
import com.m.pojo.dto.blackwhite.BlackWhiteSearchDto;
import com.m.service.IBlackWhiteService;
import com.m.utils.ResponseBuilder;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 功能：<br/>
 *
 * @author miracle
 * @see [相关类/方法]（可选）
 * @since [产品/模块版本] （可选）
 */
@RestController
@RequestMapping("/api/v1/blackWhite")
@Api(tags = "黑白名单")
public class BlackWhiteController {
    private static final Logger LOG = LoggerFactory.getLogger(BlackWhiteController.class);

    @Autowired
    private IBlackWhiteService blackWhiteService;

    @ApiOperation(value = "新增黑白名单")
    @RequestMapping(value = "/addBlackWhiteInfo", method = RequestMethod.POST)
    @ResponseBody
    public ResponseEntity addBlackWhiteInfo(@Valid @RequestBody AddBlackWhiteInfoDto addBlackWhiteInfoDto, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            FieldError fieldError = bindingResult.getFieldError();
            if (fieldError != null) {
                return ResponseBuilder.build(WebReturnCode.INVALID_PARAMS, fieldError.getDefaultMessage(), "");
            }
        }
        try {
            blackWhiteService.addBlackWhiteInfo(addBlackWhiteInfoDto);
            return ResponseBuilder.build(WebReturnCode.SUCCEED,"success", null);
        } catch (Exception e) {
            LOG.error("新增黑白名单失败:" + e.getMessage(), e);
            return ResponseBuilder.build(WebReturnCode.FAILED,"新增黑白名单失败:" + e.getMessage(), null);
        }
    }

    @ApiOperation(value = "删除黑白名单")
    @RequestMapping(value = "/delBlackWhiteInfo", method = RequestMethod.GET)
    @ResponseBody
    public ResponseEntity delBlackWhiteInfo(@RequestParam(value = "blackeWhiteId", required = true) Long blackeWhiteId) {
        try {
            blackWhiteService.delBlackWhiteInfo(blackeWhiteId);
            return ResponseBuilder.build(WebReturnCode.SUCCEED,"success", null);
        } catch (Exception e) {
            LOG.error("删除黑白名单:" + e.getMessage(), e);
            return ResponseBuilder.build(WebReturnCode.FAILED,"删除黑白名单:" + e.getMessage(), null);
        }
    }

    @ApiOperation(value = "查询黑白名单")
    @RequestMapping(value = "/getBlackWhiteInfo", method = RequestMethod.POST)
    @ResponseBody
    public ResponseEntity<List<BlackWhiteInfoDto>> getBlackWhiteInfo(@RequestBody BlackWhiteSearchDto blackWhiteSearchDto) {
        try {
            // 查询黑白名单
            return ResponseBuilder.build(WebReturnCode.SUCCEED,"success", blackWhiteService.getBlackWhiteInfo(blackWhiteSearchDto));
        } catch (Exception e) {
            LOG.error("查询黑白名单失败:" + e.getMessage(), e);
            return ResponseBuilder.build(WebReturnCode.FAILED,"查询黑白名单失败:" + e.getMessage(), null);
        }
    }
}
